﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using fsw.Common;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;


namespace fsw.Web.UI
{
    public partial class ManagePage : System.Web.UI.Page
    {
        public ManagePage()
        {
            this.Load += new EventHandler(ManagePage_Load);
        }
        /// <summary>
        /// 判断管理员是否登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagePage_Load(object sender, EventArgs e)
        {
            //判断管理员是否登录
            if (!IsAdminLogin())
            {
                HttpContext.Current.Response.Write("<script>parent.location.href='/login.aspx'</script>");
                HttpContext.Current.Response.End();
            }
        }
        /// <summary>
        /// 检查cookies是否登录
        /// </summary>
        /// <returns></returns>
        public bool IsAdminLogin()
        {
            //如果Session为Null
            if (HttpContext.Current.Session[DTKeys.SESSION_ADMIN_INFO] != null)
            {
                return true;
            }
            else
            {
                //检查Cookies
                string adminname = Utils.GetCookie("AdminName", "fsw");
                string adminpwd = Utils.GetCookie("AdminPwd", "fsw");
                if (adminname != "" && adminpwd != "")
                {
                    BLL.Admin bll = new BLL.Admin();
                    Model.Admin model = bll.GetModel(adminname, adminpwd);
                    if (model != null)
                    {
                        HttpContext.Current.Session[DTKeys.SESSION_ADMIN_INFO] = model;
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 管理员信息
        /// </summary>
        /// <returns></returns>
        public Model.Admin GetAdminInfo()
        {
            if (IsAdminLogin())
            {
                Model.Admin model = Session[DTKeys.SESSION_ADMIN_INFO] as Model.Admin;
                if (model != null)
                {
                    return model;
                }
            }
            return null;
        }
        /// <summary>
        /// 项目权限
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool UserProjectLevel(int Id)
        {
            Model.Admin model = GetAdminInfo();
            if (model.TypeId == 1) return true;
            BLL.Project bll = new BLL.Project();
            return bll.ExistsPid(model.Id, Id);
        }
        /// <summary>
        /// ajax请求事例
        /// </summary>
        /// <param name="state"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected string JsonErr(string state, string msg)
        {
            Model.JsonError jsonerror = new Model.JsonError();
            jsonerror.state = state;
            jsonerror.msg = msg;
            return JsonHelper.ObjectToJSON(jsonerror);
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Ucontent"></param>
        protected void AdminAddLog(int Userid, string UserName, string Ucontent)
        {
            BLL.Admin_Log bll = new BLL.Admin_Log();
            Model.Admin_Log model_log = new Model.Admin_Log();
            model_log.UserId = Userid;
            model_log.UserName = UserName;
            model_log.UserIP = DTRequest.GetIP();
            model_log.AddDate = Utils.ConvertDateTimeInt(DateTime.Now);
            model_log.Remark = Ucontent;
            model_log.UserAgent = DTRequest.GetUserAgent();
            bll.Add(model_log);
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Ucontent"></param>
        protected void AdminAddLog(string UserName, string Ucontent)
        {
            Model.Admin model = GetAdminInfo();
            AdminAddLog(model.Id, UserName, Ucontent);
        }
        /// <summary>
        /// 普通管理员项目权限
        /// </summary>
        /// <returns></returns>
        public string UserPossLevel()
        {
            Model.Admin model = GetAdminInfo();
            if (model.TypeId > 1)
            {
                return " And EXISTS(select pID from " + DTKeys.DATA_BASE_PREFIX + "Admin_Project where " + DTKeys.DATA_BASE_PREFIX + "Project.id=Pid and userid=" + model.Id + ")";
            }
            return "";
        }
    }
}
